package com.base.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.service.SubscribersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.CompanyNews;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.CompanyNewsService;
import com.base.service.UsersService;
import com.base.utils.Produce;

/**
 * 发现控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/app_companynews")
public class app_CompanyNewsController {
	@Autowired
	private SubscribersService subscribersService;
	@Autowired
	private CompanyNewsService companyNewsService;
	@Autowired
	private UsersService usersService;
	

	// 企业动态
	@RequestMapping(value = "/getCompanynews", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView getCompanynews(HttpServletRequest request, HttpServletResponse response,
			String Mobile, String ParentId) throws Exception {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();

		//TODO 判断参数
		if(ParentId == null || ParentId.equals("")) {
			System.out.println("参数异常");
			request.setAttribute("type", 0);
			request.setAttribute("errorstype", 0);
			request.setAttribute("nowtime", Produce.getNowDate());
            modelAndView.setViewName("jsp/app_CompanyNews");
			return modelAndView;
		}
		
		System.out.println(Mobile);
		
		//取到手机号用户的id
		List<String> subscribersId = subscribersService.selectByMobileAndParentId(Mobile, ParentId);
		Subscribers subscribers = null;
		
		if(subscribersId != null && subscribersId.size()>0) {
			//手机号所有信息
			subscribers = subscribersService.selectByPrimaryKey(subscribersId.get(0));
		}else {
			System.out.println("未查询到用户信息");
        	 request.setAttribute("type", 0);
        	 request.setAttribute("errorstype", 1);
        	 request.setAttribute("nowtime", Produce.getNowDate());
             modelAndView.setViewName("jsp/app_CompanyNews");
             return modelAndView;
		}

		
		//用户所属商家
		Users users = usersService.selectByPrimaryKey(subscribers.getShopid());
		List<CompanyNews> companyNews = new ArrayList<CompanyNews>();
		String adminid = "7dd9a07e-9cef-4702-a165-b7033861c1b7";
		
		
		
		if(users.getRole() == 1) {
			companyNews = companyNewsService.selectByRoleTOne(adminid, subscribers.getShopid());
			for(CompanyNews s : companyNews) {
				s.setUsers(null);
			}
		}
        if(users.getRole() == 2) {
        	companyNews = companyNewsService.selectByRoleTwo(adminid, subscribers.getParentid(), subscribers.getShopid());
        	for(CompanyNews s : companyNews) {
				s.setUsers(null);
			}
        }
        
        if(companyNews != null && companyNews.size()>0) {
        	
        	for(CompanyNews s : companyNews) {
        		Users u = usersService.selectByPrimaryKey(s.getParentid());
        		
        		s.setUsersLogo(u.getAgentlogo());
        		
        		if(users.getRole() == 1) {
        			if(u.getRole() == 0) {
            			s.setNewsName("官方动态");
            		}
            		if(u.getRole() == 1) {
            			s.setNewsName("商家动态");
            		}
        		}
        		if(users.getRole() == 2) {
        			if(u.getRole() == 0 || u.getRole() == 1) {
            			s.setNewsName("官方动态");
            		}
            		if(u.getRole() == 1) {
            			s.setNewsName("商家动态");
            		}
        		}
        		
        		if(s.getContent().length()>100) {
        			s.setNewsBreviary(s.getContent().substring(0, 100)+"...");
        		}else {
        			s.setNewsBreviary(s.getContent());
        		}
        	}
        	request.setAttribute("list", companyNews);
        	request.setAttribute("type", 1);
        	modelAndView.setViewName("jsp/app_CompanyNews");
        	return modelAndView;
        }else {
        	 request.setAttribute("logo", users.getAgentlogo());
        	 request.setAttribute("type", 0);
        	 request.setAttribute("nowtime", Produce.getNowDate());
             modelAndView.setViewName("jsp/app_CompanyNews");
     		 return modelAndView;
        }
	}
	
	@RequestMapping("/app_CompanyNewsDetails")
	public ModelAndView index(HttpServletRequest request,String id) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		
		if (id == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		
		CompanyNews companyNews = companyNewsService.selectByPrimaryKey(id);
		if(companyNews != null) {
			request.setAttribute("id", id);
			request.setAttribute("title", companyNews.getTitle());
			request.setAttribute("content", companyNews.getContent());
			request.setAttribute("username", companyNews.getUsername());
			request.setAttribute("url", companyNews.getUrl());
			request.setAttribute("createtime", companyNews.getCreatetime());
			request.setAttribute("contentpic", companyNews.getContentpic());
			
			System.out.println(companyNews.getContentpic());
			
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/app_CompanyNewsDetails");
			return modelAndView;
		}
		
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}